/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.consumer.modular.structure.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.consumer.modular.structure.org.entity.ConsumerOrg;
import vip.xiaonuo.consumer.modular.structure.position.entity.ConsumerPosition;
import vip.xiaonuo.consumer.modular.structure.user.entity.ConsumerUser;
import vip.xiaonuo.consumer.modular.structure.user.param.*;
import vip.xiaonuo.consumer.modular.structure.user.result.ConsumerUserOwnResourceResult;
import vip.xiaonuo.consumer.modular.structure.user.result.ConsumerUserRoleResult;
import vip.xiaonuo.consumer.modular.structure.user.service.ConsumerUserService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import java.io.IOException;
import java.util.List;

/**
 * 人员控制器
 *
 * @author xuyuxiang
 * @date 2022/4/22 9:34
 **/
@Tag(name = "人员控制器")
@RestController
@Validated
public class ConsumerUserController {

    @Resource
    private ConsumerUserService consumerUserService;

    /**
     * 获取人员分页
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员分页")
    @SaCheckPermission("/consumer/structure/user/page")
    @GetMapping("/consumer/structure/user/page")
    public CommonResult<Page<ConsumerUser>> page(ConsumerUserPageParam consumerUserPageParam) {
        return CommonResult.data(consumerUserService.page(consumerUserPageParam));
    }

    /**
     * 添加人员
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:47
     */
    @Operation(summary = "添加人员")
    @CommonLog("添加人员")
    @SaCheckPermission("/consumer/structure/user/add")
    @PostMapping("/consumer/structure/user/add")
    public CommonResult<String> add(@RequestBody @Valid ConsumerUserAddParam consumerUserAddParam) {
        consumerUserService.add(consumerUserAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑人员
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:47
     */
    @Operation(summary = "编辑人员")
    @CommonLog("编辑人员")
    @SaCheckPermission("/consumer/structure/user/edit")
    @PostMapping("/consumer/structure/user/edit")
    public CommonResult<String> edit(@RequestBody @Valid ConsumerUserEditParam consumerUserEditParam) {
        consumerUserService.edit(consumerUserEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除人员
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "删除人员")
    @CommonLog("删除人员")
    @SaCheckPermission("/consumer/structure/user/delete")
    @PostMapping("/consumer/structure/user/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<ConsumerUserIdParam> consumerUserIdParamList) {
        consumerUserService.delete(consumerUserIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取人员详情
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员详情")
    @SaCheckPermission("/consumer/structure/user/detail")
    @GetMapping("/consumer/structure/user/detail")
    public CommonResult<ConsumerUser> detail(@Valid ConsumerUserIdParam consumerUserIdParam) {
        return CommonResult.data(consumerUserService.detail(consumerUserIdParam));
    }

    /**
     * 禁用人员
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @Operation(summary = "禁用人员")
    @CommonLog("禁用人员")
    @SaCheckPermission("/consumer/structure/user/disableUser")
    @PostMapping("/consumer/structure/user/disableUser")
    public CommonResult<String> disableUser(@RequestBody ConsumerUserIdParam consumerUserIdParam) {
        consumerUserService.disableUser(consumerUserIdParam);
        return CommonResult.ok();
    }

    /**
     * 启用人员
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @Operation(summary = "启用人员")
    @CommonLog("启用人员")
    @SaCheckPermission("/consumer/structure/user/enableUser")
    @PostMapping("/consumer/structure/user/enableUser")
    public CommonResult<String> enableUser(@RequestBody @Valid ConsumerUserIdParam consumerUserIdParam) {
        consumerUserService.enableUser(consumerUserIdParam);
        return CommonResult.ok();
    }

    /**
     * 重置人员密码
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @Operation(summary = "重置人员密码")
    @CommonLog("重置人员密码")
    @SaCheckPermission("/consumer/structure/user/resetPassword")
    @PostMapping("/consumer/structure/user/resetPassword")
    public CommonResult<String> resetPassword(@RequestBody @Valid ConsumerUserIdParam consumerUserIdParam) {
        consumerUserService.resetPassword(consumerUserIdParam);
        return CommonResult.ok();
    }

    /**
     * 人员拥有角色
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员拥有角色")
    @SaCheckPermission("/consumer/structure/user/ownRole")
    @GetMapping("/consumer/structure/user/ownRole")
    public CommonResult<List<String>> ownRole(@Valid ConsumerUserIdParam consumerUserIdParam) {
        return CommonResult.data(consumerUserService.ownRole(consumerUserIdParam));
    }

    /**
     * 给人员授权角色
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "给人员授权角色")
    @CommonLog("给人员授权角色")
    @SaCheckPermission("/consumer/structure/user/grantRole")
    @PostMapping("/consumer/structure/user/grantRole")
    public CommonResult<String> grantRole(@RequestBody @Valid ConsumerUserGrantRoleParam consumerUserGrantRoleParam) {
        consumerUserService.grantRole(consumerUserGrantRoleParam);
        return CommonResult.ok();
    }

    /**
     * 获取用户拥有资源
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取用户拥有资源")
    @SaCheckPermission("/consumer/structure/user/ownResource")
    @GetMapping("/consumer/structure/user/ownResource")
    public CommonResult<ConsumerUserOwnResourceResult> ownResource(@Valid ConsumerUserIdParam consumerUserIdParam) {
        return CommonResult.data(consumerUserService.ownResource(consumerUserIdParam));
    }

    /**
     * 给用户授权资源
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "给用户授权资源")
    @CommonLog("给用户授权资源")
    @SaCheckPermission("/consumer/structure/user/grantResource")
    @PostMapping("/consumer/structure/user/grantResource")
    public CommonResult<String> grantResource(@RequestBody @Valid ConsumerUserGrantResourceParam consumerUserGrantResourceParam) {
        consumerUserService.grantResource(consumerUserGrantResourceParam);
        return CommonResult.ok();
    }

    /**
     * 人员导出
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "人员导出")
    @CommonLog("人员导出")
    @SaCheckPermission("/consumer/structure/user/export")
    @GetMapping(value = "/consumer/structure/user/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportUser(ConsumerUserExportParam consumerUserExportParam, HttpServletResponse response) throws IOException {
        consumerUserService.exportUser(consumerUserExportParam, response);
    }

    /**
     * 按模板导出人员个人信息
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "导出人员个人信息")
    @CommonLog("导出人员个人信息")
    @GetMapping(value = "/consumer/structure/user/exportUserInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportUserInfo(ConsumerUserIdParam consumerUserIdParam, HttpServletResponse response) throws IOException {
        consumerUserService.exportUserInfo(consumerUserIdParam, response);
    }

    /* ====人员部分所需要用到的选择器==== */

    /**
     * 获取机构树选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取机构树选择器")
    @SaCheckPermission("/consumer/structure/user/orgTreeSelector")
    @GetMapping("/consumer/structure/user/orgTreeSelector")
    public CommonResult<List<Tree<String>>> orgTreeSelector() {
        return CommonResult.data(consumerUserService.orgTreeSelector());
    }

    /**
     * 获取机构列表选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取机构列表选择器")
    @SaCheckPermission("/consumer/structure/user/orgListSelector")
    @GetMapping("/consumer/structure/user/orgListSelector")
    public CommonResult<Page<ConsumerOrg>> orgListSelector(ConsumerUserSelectorOrgListParam consumerUserSelectorOrgListParam) {
        return CommonResult.data(consumerUserService.orgListSelector(consumerUserSelectorOrgListParam));
    }

    /**
     * 获取岗位选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取岗位选择器")
    @SaCheckPermission("/consumer/structure/user/positionSelector")
    @GetMapping("/consumer/structure/user/positionSelector")
    public CommonResult<Page<ConsumerPosition>> positionSelector(ConsumerUserSelectorPositionParam consumerUserSelectorPositionParam) {
        return CommonResult.data(consumerUserService.positionSelector(consumerUserSelectorPositionParam));
    }

    /**
     * 获取角色选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取角色选择器")
    @SaCheckPermission("/consumer/structure/user/roleSelector")
    @GetMapping("/consumer/structure/user/roleSelector")
    public CommonResult<Page<ConsumerUserRoleResult>> roleSelector(ConsumerUserSelectorRoleParam consumerUserSelectorRoleParam) {
        return CommonResult.data(consumerUserService.roleSelector(consumerUserSelectorRoleParam));
    }

    /**
     * 获取人员选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员选择器")
    @SaCheckPermission("/consumer/structure/user/userSelector")
    @GetMapping("/consumer/structure/user/userSelector")
    public CommonResult<Page<ConsumerUser>> userSelector(ConsumerUserSelectorUserParam consumerUserSelectorUserParam) {
        return CommonResult.data(consumerUserService.userSelector(consumerUserSelectorUserParam));
    }
}
